<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>电影管理</title>
    <link rel="stylesheet" href="static/elementui.css">
    <script src="static/vue@2.6.14.js"></script>
    <script src="static/elementui.js"></script>
    <script src="static/axios.min.js"></script>
    <style>
        body {
            padding: 20px;
        }

        .toolbar {
            margin-bottom: 20px;
        }

        .dialog-footer {
            text-align: right;
            margin-top: 20px;
        }

        .el-table .el-button {
            margin-bottom: 10px;
        }

        .poster-preview {
            width: 100px;
            height: 150px;
            margin-right: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            overflow: hidden;
        }

        .poster-preview img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body>
<div id="app">
    <el-card>
        <div class="toolbar">
            <el-input placeholder="搜索电影名称" v-model="searchKeyword" style="width: 200px;"></el-input>
            <el-button type="primary" size="small" @click="handleAddMovie">添加电影</el-button>
        </div>
        <el-table :data="movies" stripe>
            <el-table-column prop="id" label="ID"></el-table-column>
            <el-table-column prop="title" label="电影名称"></el-table-column>
            <el-table-column prop="description" label="描述"></el-table-column>
            <el-table-column prop="releaseDate" label="上映日期"></el-table-column>
            <el-table-column prop="duration" label="时长"></el-table-column>
            <el-table-column prop="rating" label="评分"></el-table-column>
            <el-table-column prop="poster" label="海报">
                <template slot-scope="scope">
                    <el-image
                            style="width: 100px; height: 150px"
                            :src="scope.row.poster"
                            :preview-src-list="[scope.row.poster]">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column prop="createdAt" label="创建时间"></el-table-column>
            <el-table-column fixed="right" label="操作" width="200">
                <template slot-scope="scope">
                    <el-button @click="handleEdit(scope.row)" type="primary" size="small">编辑</el-button>
                    <el-button @click="handleDelete(scope.row)" type="danger" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="block">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 20, 50]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalMovies">
            </el-pagination>
        </div>
    </el-card>

    <el-dialog :title="isEditMode ? '编辑电影' : '添加电影'" :visible.sync="dialogVisible">
        <el-form :model="currentMovie" :rules="rules" ref="movieForm" label-width="100px">
            <el-form-item label="电影名称" prop="title">
                <el-input v-model="currentMovie.title" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="description">
                <el-input v-model="currentMovie.description" type="textarea" rows="3" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="上映日期" prop="releaseDate">
                <el-date-picker v-model="currentMovie.releaseDate" type="date" placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="时长" prop="duration">
                <el-input v-model.number="currentMovie.duration" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="评分" prop="rating">
                <el-input v-model.number="currentMovie.rating" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="海报" prop="poster">
                <el-upload
                        class="avatar-uploader"
                        action="/upload"
                        :show-file-list="false"
                        :on-success="handlePosterSuccess"
                        :before-upload="beforePosterUpload">
                    <img v-if="currentMovie.poster" :src="currentMovie.poster" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisiblePreview">
                    <el-image :src="dialogImageUrl"></el-image>
                </el-dialog>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleSave">保存</el-button>
        </div>
    </el-dialog>
</div>

<script>
    new Vue({
        el: '#app',
        data() {
            return {
                searchKeyword: '',
                movies: [],
                totalMovies: 0,
                pageSize: 5,
                currentPage: 1,
                dialogVisible: false,
                currentMovie: {},
                isEditMode: false,
                dialogVisiblePreview: false,
                dialogImageUrl: '',
                rules: {
                    title: [
                        {required: true, message: '请输入电影名称', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '请输入描述', trigger: 'blur'}
                    ],
                    releaseDate: [
                        {required: true, message: '请选择上映日期', trigger: 'change'}
                    ],
                    duration: [
                        {required: true, message: '请输入时长', trigger: 'blur'},
                        {type: 'number', message: '时长必须为数字', trigger: 'blur'}
                    ],
                    rating: [
                        {required: true, message: '请输入评分', trigger: 'blur'},
                        {type: 'number', message: '评分必须为数字', trigger: 'blur'}
                    ],
                    poster: [
                        {required: true, message: '请上传海报', trigger: 'blur'}
                    ]
                }
            };
        },
        watch: {
            searchKeyword(newVal, oldVal) {
                if (newVal !== oldVal) {
                    this.currentPage = 1; // 重置当前页码为1
                    this.fetchMovies(); // 重新获取电影数据
                }
            },
            currentPage(newVal, oldVal) {
                if (newVal !== oldVal) {
                    this.fetchMovies(); // 重新获取电影数据
                }
            },
            pageSize(newVal, oldVal) {
                if (newVal !== oldVal) {
                    this.fetchMovies(); // 重新获取电影数据
                }
            }
        },
        methods: {
            fetchMovies() {
                axios.get('/movie?operation=findMoviesByPageAndTitle', {
                    params: {
                        keyword: this.searchKeyword,
                        pageNum: this.currentPage,
                        pageSize: this.pageSize
                    }
                }).then(response => {
                    const {code, data} = response.data;
                    if (code === 200) {
                        this.movies = data.data; // 更新数据字段为 data.data
                        this.totalMovies = data.total;
                    } else {
                        this.$message.error('获取电影列表失败');
                    }
                }).catch(error => {
                    console.error('获取电影列表失败:', error);
                    this.$message.error('获取电影列表失败');
                });
            },
            handleAddMovie() {
                this.isEditMode = false;
                this.currentMovie = {
                    title: '',
                    description: '',
                    releaseDate: '',
                    duration: '',
                    rating: '',
                    poster: ''
                };
                this.dialogVisible = true;
            },
            handleEdit(movie) {
                this.isEditMode = true;
                this.currentMovie = Object.assign({}, movie);
                this.dialogVisible = true;
            },
            handleSave() {
                this.$refs.movieForm.validate(valid => {
                    if (valid) {
                        const url = this.isEditMode ? '/movie?operation=updateMovie' : '/movie?operation=addMovie';
                        axios.post(url, this.currentMovie)
                            .then(response => {
                                if (response.data.code === 200) {
                                    this.$message.success(this.isEditMode ? '电影信息更新成功' : '电影添加成功');
                                    this.fetchMovies();
                                } else {
                                    this.$message.error(this.isEditMode ? '电影信息更新失败' : '电影添加失败');
                                }
                            })
                            .catch(error => {
                                console.error(this.isEditMode ? '电影信息更新失败:' : '电影添加失败:', error);
                                this.$message.error(this.isEditMode ? '电影信息更新失败' : '电影添加失败');
                            });
                        this.dialogVisible = false;
                    }
                });
            },
            handleDelete(movie) {
                this.$confirm('此操作将永久删除该电影, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post('/movie?operation=deleteMovie&id=' + movie.id)
                        .then(response => {
                            if (response.data.code === 200) {
                                this.$message.success('电影删除成功');
                                this.fetchMovies();
                            } else {
                                this.$message.error('电影删除失败');
                            }
                        })
                        .catch(error => {
                            console.error('电影删除失败:', error);
                            this.$message.error('电影删除失败');
                        });
                }).catch(() => {
                    this.$message.info('已取消删除');
                });
            },
            handleSizeChange(val) {
                this.pageSize = val;
            },
            handleCurrentChange(val) {
                this.currentPage = val;
            },
            handlePosterSuccess(response, file) {
                    this.currentMovie.poster = response.data;
                    this.$message.success('海报上传成功');
            },
            beforePosterUpload(file) {
                return true;
            },
            handlePreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisiblePreview = true;
            },
            handleRemove(file, fileList) {
                this.currentMovie.poster = '';
            }
        },
        mounted() {
            this.fetchMovies();
        }
    });
</script>

</body>
</html>